package com.gss.common.util;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;

import com.gss.armory.domain.CompanyDetailsDTO;
import com.gss.armory.domain.DocGeneralDetailsDTO;
import com.gss.armory.domain.DocGeneralDetailsDTO.DocumentFlowStatus;
import com.gss.armory.domain.DocumentCustodianDetailsDTO;
import com.gss.armory.domain.DocumentLinkDTO;
import com.gss.armory.domain.DocumentLinkDTO.DocLinkType;
import com.gss.armory.domain.DocumentWorkflowDetailsDTO;
import com.gss.armory.domain.DocumentWorkflowDetailsDTO.WorkflowEventStatus;
import com.gss.armory.domain.DocumentWorkflowDetailsDTO.WorkflowStatus;
import com.gss.armory.domain.LocationDetailsDTO;
import com.gss.armory.domain.UserActivityGroup;
import com.gss.armory.domain.UserActivityGroup.DocActivityType;
import com.gss.armory.form.DocDetailsForm;
import com.gss.armory.form.DocRenewalDetailsForm;
import com.gss.armory.form.DocumentCustodianDetailsForm;
import com.gss.armory.form.UserActivityForm;
import com.gss.common.domain.CommonDetailsDTO;
import com.gss.security.domain.UserDetailsDTO;
import com.gss.security.persistence.AuthenticationDAO;

public class ApplicationUtilities {
	
	Logger logger = Logger.getLogger(ApplicationUtilities.class);

	@Autowired 
	AuthenticationDAO authenticationDAO;
	
	public static void populateCommonDetails(CommonDetailsDTO commonDetailsDTO,boolean isNew){
		 UserDetails userDetails = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		 if(isNew){
			 commonDetailsDTO.setVersion(1);
			 commonDetailsDTO.setCreatedDate(new Date());
			 commonDetailsDTO.setStrCreatedAuthor(userDetails.getUsername());
		 }else{
			 commonDetailsDTO.setVersion(commonDetailsDTO.getVersion()+1);
			 commonDetailsDTO.setUpdatedDate(new Date());
			 commonDetailsDTO.setStrUpdatedAuthor(userDetails.getUsername());
		 }
	}
	
	public static String getCurrentUser(){
		return ((UserDetailsDTO) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getUsername();
	}
	
	public static String getCurrentUserEmail(){
		return ((UserDetailsDTO) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getEmailId();
	}
	
	public static int getCurrentUserCompanyId(){
		if(null == ((UserDetailsDTO) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getCdId())
			return 0;
		else
			return ((UserDetailsDTO) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getCdId();
	}
	
	public static long getCurrentUserBUId(){
		return ((UserDetailsDTO) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getBuId();
	}
	
	public static boolean checkIfNullOrBlank(String strToCheck){
		if(null == strToCheck || "".equals(strToCheck.trim()))
			return true;
		else
			return false;
	}
	
	public static DocGeneralDetailsDTO getDocumentGenDTOfromDocUploadForm(DocDetailsForm docDetailsForm){
		DocGeneralDetailsDTO docGeneralDetailsDTO = new DocGeneralDetailsDTO();
		docGeneralDetailsDTO.setDocCategoryId(docDetailsForm.getDocCategoryId());
		docGeneralDetailsDTO.setRenewable(docDetailsForm.isRenewable());
		docGeneralDetailsDTO.setDocSubCategoryId(docDetailsForm.getDocSubCategoryId());
		docGeneralDetailsDTO.setDocLocId(docDetailsForm.getDocLocId());
		docGeneralDetailsDTO.setDocDepttId(docDetailsForm.getDocDepttId());
		docGeneralDetailsDTO.setDocTypeId(docDetailsForm.getDocTypeId());
		docGeneralDetailsDTO.setDocName(docDetailsForm.getDocName());
		docGeneralDetailsDTO.setDocDesc(docDetailsForm.getDocDesc());
		docGeneralDetailsDTO.setDocOwnerId(docDetailsForm.getDocOwnerId());
		docGeneralDetailsDTO.setDocOwnerName(docDetailsForm.getDocOwnerName());
		docGeneralDetailsDTO.setDocValidityStartDate(docDetailsForm.getDocValidityStartDate());
		docGeneralDetailsDTO.setDocValidityEndDate(docDetailsForm.getDocValidityEndDate());	
		docGeneralDetailsDTO.setFirstEscalationPeriod(docDetailsForm.getFirstEscalationPeriod());
		docGeneralDetailsDTO.setSecondEscalationPeriod(docDetailsForm.getSecondEscalationPeriod());
		docGeneralDetailsDTO.setThirdEscalationPeriod(docDetailsForm.getThirdEscalationPeriod());	
		docGeneralDetailsDTO.setDocIssuingAuthority(docDetailsForm.getDocIssuingAuthority());
		docGeneralDetailsDTO.setDocEnteredWith(docDetailsForm.getDocEnteredWith());
		if(!ApplicationUtilities.checkIfNullOrBlank(docDetailsForm.getDocumentStatus())){
			docGeneralDetailsDTO.setDocumentStatus(DocumentFlowStatus.valueOf(docDetailsForm.getDocumentStatus()));
		}
		populateCommonDetails(docGeneralDetailsDTO, true);
		return docGeneralDetailsDTO;
	}

	public static DocDetailsForm getDocumentDetailfromDocGeneralDetailDTO(DocGeneralDetailsDTO docGeneralDetailsDTO, String custodianName){
		
		DocDetailsForm docDetailsForm = new DocDetailsForm();		
		docDetailsForm.setDocCategoryId(docGeneralDetailsDTO.getDocCategoryId());
		docDetailsForm.setDocCategory(ApplicationMaps.DOCUMENT_CAT_ID_MAP.get(docGeneralDetailsDTO.getDocCategoryId()));
		docDetailsForm.setDocSubCategoryId(docGeneralDetailsDTO.getDocSubCategoryId());
		docDetailsForm.setDocSubCategory(ApplicationMaps.DOCUMENT_SUB_CAT_ID_MAP.get(docGeneralDetailsDTO.getDocSubCategoryId()));
		docDetailsForm.setDocTypeId(docGeneralDetailsDTO.getDocTypeId());
		docDetailsForm.setDocType(ApplicationMaps.DOCUMENT_TYPE_ID_NAME_MAP.get(docGeneralDetailsDTO.getDocTypeId()));
		docDetailsForm.setDocName(docGeneralDetailsDTO.getDocName());
		docDetailsForm.setDocDesc(docGeneralDetailsDTO.getDocDesc());
		docDetailsForm.setDocDeptt(ApplicationMaps.DEPTID_DEPTSNAME_MAP.get(docGeneralDetailsDTO.getDocDepttId()));
		docDetailsForm.setDocOwnerId(docGeneralDetailsDTO.getDocOwnerId());
		docDetailsForm.setDocLoc(ApplicationMaps.LOCID_LOCNAME_MAP.get(Long.valueOf(docGeneralDetailsDTO.getDocLocId())));
		docDetailsForm.setDocOwnerName(docGeneralDetailsDTO.getDocOwnerName());
		docDetailsForm.setDocValidityStartDate(docGeneralDetailsDTO.getDocValidityStartDate());
		docDetailsForm.setDocValidityEndDate(docGeneralDetailsDTO.getDocValidityEndDate());
		docDetailsForm.setDocId(docGeneralDetailsDTO.getDocId());
		docDetailsForm.setFirstEscalationPeriod(docGeneralDetailsDTO.getFirstEscalationPeriod());
		docDetailsForm.setSecondEscalationPeriod(docGeneralDetailsDTO.getSecondEscalationPeriod());
		docDetailsForm.setThirdEscalationPeriod(docGeneralDetailsDTO.getThirdEscalationPeriod());
		docDetailsForm.setDocCode(docGeneralDetailsDTO.getDocCode());
		docDetailsForm.setDocDepttId((int) docGeneralDetailsDTO.getDocDepttId());
		docDetailsForm.setDocLocId((int) docGeneralDetailsDTO.getDocLocId());
		docDetailsForm.setRenewable(docGeneralDetailsDTO.isRenewable());
		docDetailsForm.setDocIssuingAuthority(docGeneralDetailsDTO.getDocIssuingAuthority());
		docDetailsForm.setDocEnteredWith(docGeneralDetailsDTO.getDocEnteredWith());
		docDetailsForm.setDocumentStatus(docGeneralDetailsDTO.getDocumentStatus().toString());
		docDetailsForm.setDocCompanyId(docGeneralDetailsDTO.getDocCompanyId());
		if(null != custodianName)
			docDetailsForm.setCustodianName(custodianName);
//		docDetailsForm.setMetadataValueList(docGeneralDetailsDTO.getMetadataValueList());
		return docDetailsForm;
	}

	
	public static DocumentWorkflowDetailsDTO getDocumentWorkflowDetDTOfromDocUploadForm(
		DocDetailsForm docDetailsForm ,DocGeneralDetailsDTO docGeneralDetailsDTO) {
		DocumentWorkflowDetailsDTO documentWorkflowDetailsDTO = new DocumentWorkflowDetailsDTO();
		documentWorkflowDetailsDTO.setDocId(docGeneralDetailsDTO.getDocId());
		documentWorkflowDetailsDTO.setDocName(docGeneralDetailsDTO.getDocName());
		documentWorkflowDetailsDTO.setWfAssignedUser(getCurrentUser());
		documentWorkflowDetailsDTO.setWfStatus(WorkflowStatus.PFS);
		documentWorkflowDetailsDTO.setWfEventStatus(WorkflowEventStatus.OPEN);
		documentWorkflowDetailsDTO.setRemarks("Created");
		documentWorkflowDetailsDTO.setCreatorRemarks("Submit document for review.");
		populateCommonDetails(documentWorkflowDetailsDTO, true);
		return documentWorkflowDetailsDTO;
	}
	
	/*public static DocumentRenewelDTO getDocRenewalDTOFromDocUploadForm(DocumentUploadForm docUploadForm){
		DocumentRenewelDTO documentRenewelDTO = new DocumentRenewelDTO();
		documentRenewelDTO.setDocFirstEscPrd(docUploadForm.getDocRenewDtlsForm().getDocFirstEscPrd());
		documentRenewelDTO.setDocSecondEscPrd(docUploadForm.getDocRenewDtlsForm().getDocSecondEscPrd());
		documentRenewelDTO.setDocThirdEscPrd(docUploadForm.getDocRenewDtlsForm().getDocThirdEscPrd());
		documentRenewelDTO.setDocValidStartDt(docUploadForm.getDocRenewDtlsForm().getDocValidStartDt());
		documentRenewelDTO.setDocValidEndDt(docUploadForm.getDocRenewDtlsForm().getDocValidEndDt());
		populateCommonDetails(documentRenewelDTO, true);
		return documentRenewelDTO;
	}
	
	public static DocumentVaultDetailsDTO getDocVaultDetailsDTOFromDocUploadForm(DocumentUploadForm docUploadForm){
		DocumentVaultDetailsDTO docVaultDetailsDTO = new DocumentVaultDetailsDTO();
		docVaultDetailsDTO.setCustBuLoc(docUploadForm.getDocPhysicalLocForm().getCustBuLoc());
		docVaultDetailsDTO.setCustDept(docUploadForm.getDocPhysicalLocForm().getCustDept());
		docVaultDetailsDTO.setCustDesignation(docUploadForm.getDocPhysicalLocForm().getCustDesig());
		docVaultDetailsDTO.setCustId(docUploadForm.getDocPhysicalLocForm().getCustId());
		docVaultDetailsDTO.setCustName(docUploadForm.getDocPhysicalLocForm().getCustName());
		docVaultDetailsDTO.setVaultLoc(docUploadForm.getDocPhysicalLocForm().getVaultLoc());
		docVaultDetailsDTO.setVaultNumber(docUploadForm.getDocPhysicalLocForm().getVaultNumber());
		populateCommonDetails(docVaultDetailsDTO, true);
		return docVaultDetailsDTO;
	}
	
	public static List<DocumentMetaDataAttrValueDTO> getMetaAttrValueDTOListFromDocUploadForm(DocumentUploadForm documentUploadForm){
		List<DocumentMetaDataAttrValueDTO> attrValueDTOList = new ArrayList<DocumentMetaDataAttrValueDTO>();
		for(DocMetaDataAttrValueForm attrValueForm : documentUploadForm.getAttDTOs()){
			DocumentMetaDataAttrValueDTO attrValueDTO = new DocumentMetaDataAttrValueDTO();
			attrValueDTO.setDmaId(attrValueForm.getAttrId());
			attrValueDTO.setDmaValue(attrValueForm.getAttrValue());
			populateCommonDetails(attrValueDTO, true);
			attrValueDTOList.add(attrValueDTO);
		}
		return attrValueDTOList;
	}
	
	public static DocumentStatusDTO getStatusDTOForNewDoc(){
		DocumentStatusDTO documentStatusDTO = new DocumentStatusDTO();
		populateCommonDetails(documentStatusDTO, true);
		documentStatusDTO.setDocStatus(DocumentStatus.Active);
		return documentStatusDTO;
	} */

	public static Collection<GrantedAuthority> getCurrentRole(){
		return ((UserDetailsDTO) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getAuthorities();
	}

	public static String generateDocumentCode(DocGeneralDetailsDTO documentDTO){
		long locId = documentDTO.getDocLocId();
		Logger logger = Logger.getLogger(ApplicationUtilities.class);
		logger.debug(locId);
		LocationDetailsDTO docLocation = ApplicationMaps.LOCID_LOC_MAP.get(locId);
		logger.debug(docLocation.getLdCode());
		CompanyDetailsDTO docCompany = docLocation.getLdCompanyId();
		StringBuffer docCode = new StringBuffer();
		docCode.append(docCompany.getCdCode());
		docCode.append("/");
		int docTypeId = documentDTO.getDocTypeId();
		docCode.append(ApplicationMaps.DOCUMENT_TYPE_ID_MAP.get(docTypeId).getDtCode());
		docCode.append("/");
		docCode.append(docLocation.getLdCode());
		docCode.append("/");
		docCode.append(documentDTO.getDocId());
		if(documentDTO.getVersion() > 1){
			docCode.append("_v"+documentDTO.getVersion());
		}
		return docCode.toString();
	}


	public static DocumentCustodianDetailsDTO getVaultDetalsFromDetailsForm(DocumentCustodianDetailsForm detailsForm) {
		DocumentCustodianDetailsDTO custodianDetailsDTO = new DocumentCustodianDetailsDTO();
		custodianDetailsDTO.setCustodianDepttId(detailsForm.getCustodianDepttId());
		custodianDetailsDTO.setCustodianDesig(detailsForm.getCustodianDesig());
		custodianDetailsDTO.setCustodianLocId(detailsForm.getCustodianLocId());
		custodianDetailsDTO.setDocId(detailsForm.getDocId());
		custodianDetailsDTO.setCustodianId(detailsForm.getCustodianId());
		custodianDetailsDTO.setVaultNumber(detailsForm.getVaultNumber());
		custodianDetailsDTO.setCustodianName(detailsForm.getCustodianName());
		custodianDetailsDTO.setVaultLocation(detailsForm.getVaultLocation());
		if(detailsForm.getVaultId() != 0)
			custodianDetailsDTO.setVaultId(detailsForm.getVaultId());
		return custodianDetailsDTO;
	}

	public static DocRenewalDetailsForm getdocrenewalDetailsFromGeneralDetails(
			DocGeneralDetailsDTO detailsDTO) {	
		DocRenewalDetailsForm detailsForm = new DocRenewalDetailsForm();
		detailsForm.setDocId(detailsDTO.getDocId());
		detailsForm.setDocDesc(detailsDTO.getDocDesc());
		detailsForm.setDocOwnerId(detailsDTO.getDocOwnerId());
		detailsForm.setDocOwnerName(detailsDTO.getDocOwnerName());
		detailsForm.setDocDepttId(detailsDTO.getDocDepttId());
		detailsForm.setDocLocId(detailsDTO.getDocLocId());
		detailsForm.setDocTypeId(detailsDTO.getDocTypeId());
		detailsForm.setRenewable(detailsDTO.isRenewable());
		detailsForm.setDocCategoryId(detailsDTO.getDocCategoryId());
		detailsForm.setDocSubCategoryId(detailsDTO.getDocSubCategoryId());
		detailsForm.setDocDeptt(ApplicationMaps.DEPTID_DEPTSNAME_MAP.get(detailsDTO.getDocDepttId()));
		detailsForm.setDocLoc(ApplicationMaps.LOCID_LOCNAME_MAP.get(Long.valueOf(detailsDTO.getDocLocId())));
		detailsForm.setDocName(detailsDTO.getDocName());
		detailsForm.setDocCategoryId(detailsDTO.getDocCategoryId());
		detailsForm.setDocCategory(ApplicationMaps.DOCUMENT_CAT_ID_MAP.get(detailsDTO.getDocCategoryId()));
		detailsForm.setDocType(ApplicationMaps.DOCUMENT_TYPE_ID_NAME_MAP.get(detailsDTO.getDocTypeId()));
		detailsForm.setDocSubCategory(ApplicationMaps.DOCUMENT_SUB_CAT_ID_MAP.get(detailsDTO.getDocSubCategoryId()));
		detailsForm.setFirstEscalationPeriod(detailsDTO.getFirstEscalationPeriod());
		detailsForm.setSecondEscalationPeriod(detailsDTO.getSecondEscalationPeriod());
		detailsForm.setThirdEscalationPeriod(detailsDTO.getThirdEscalationPeriod());
		detailsForm.setDocValidityStartDate(detailsDTO.getDocValidityStartDate());
		detailsForm.setDocValidityEndDate(detailsDTO.getDocValidityEndDate());
		detailsForm.setDocumentStatus(detailsDTO.getDocumentStatus().toString());
		detailsForm.setMetadataValueList(detailsDTO.getMetadataValueList());
		detailsForm.setDocIssuingAuthority(detailsDTO.getDocIssuingAuthority());
		detailsForm.setDocEnteredWith(detailsDTO.getDocEnteredWith());
		detailsForm.setVersion(detailsDTO.getVersion());
		return detailsForm;
	}

	public static DocGeneralDetailsDTO getDocumentGenDTOfromDocRenewForm(
			DocRenewalDetailsForm docDetailsForm) {
		DocGeneralDetailsDTO docGeneralDetailsDTO = new DocGeneralDetailsDTO();
		docGeneralDetailsDTO.setDocCategoryId(docDetailsForm.getDocCategoryId());
		docGeneralDetailsDTO.setRenewable(docDetailsForm.isRenewable());
		docGeneralDetailsDTO.setDocSubCategoryId(docDetailsForm.getDocSubCategoryId());
		docGeneralDetailsDTO.setDocLocId(docDetailsForm.getDocLocId());
		docGeneralDetailsDTO.setDocDepttId(docDetailsForm.getDocDepttId());
		docGeneralDetailsDTO.setDocTypeId(docDetailsForm.getDocTypeId());
		docGeneralDetailsDTO.setDocName(docDetailsForm.getDocName());
		docGeneralDetailsDTO.setDocDesc(docDetailsForm.getDocDesc());
		docGeneralDetailsDTO.setDocOwnerId(docDetailsForm.getDocOwnerId());
		docGeneralDetailsDTO.setDocOwnerName(docDetailsForm.getDocOwnerName());
		docGeneralDetailsDTO.setDocValidityStartDate(docDetailsForm.getDocValidityStartDate());
		docGeneralDetailsDTO.setDocValidityEndDate(docDetailsForm.getDocValidityEndDate());	
		docGeneralDetailsDTO.setFirstEscalationPeriod(docDetailsForm.getFirstEscalationPeriod());
		docGeneralDetailsDTO.setSecondEscalationPeriod(docDetailsForm.getSecondEscalationPeriod());
		docGeneralDetailsDTO.setThirdEscalationPeriod(docDetailsForm.getThirdEscalationPeriod());	
		docGeneralDetailsDTO.setDocIssuingAuthority(docDetailsForm.getDocIssuingAuthority());
		docGeneralDetailsDTO.setDocEnteredWith(docDetailsForm.getDocEnteredWith());
		populateCommonDetails(docGeneralDetailsDTO, true);
		return docGeneralDetailsDTO;
	}

	public static DocumentWorkflowDetailsDTO getDocumentWorkflowDetDTOfromRenewDocForm(
			DocRenewalDetailsForm renewalDetailsForm,
			DocGeneralDetailsDTO docGeneralDetailsDTO) {
		DocumentWorkflowDetailsDTO documentWorkflowDetailsDTO = new DocumentWorkflowDetailsDTO();
		documentWorkflowDetailsDTO.setDocId(docGeneralDetailsDTO.getDocId());
		documentWorkflowDetailsDTO.setDocName(docGeneralDetailsDTO.getDocName());
		documentWorkflowDetailsDTO.setWfAssignedUser(getCurrentUser());
		documentWorkflowDetailsDTO.setWfStatus(WorkflowStatus.PFS);
		documentWorkflowDetailsDTO.setWfEventStatus(WorkflowEventStatus.OPEN);
		documentWorkflowDetailsDTO.setRemarks("Pending for submission");
		populateCommonDetails(documentWorkflowDetailsDTO, true);
		return documentWorkflowDetailsDTO;
	}

	public static HashMap<String,List<UserActivityGroup>> getUserActivityMapping(
			UserActivityForm activityForm, String[] oldPrintGroup, String[] oldViewGroup) {
		
		String[] printGroup = activityForm.getPrintGroup();
		String[] viewGroup = activityForm.getViewGroup();
		
		Set<String> userPrintSet = new HashSet<String>();
		if(printGroup!=null&&printGroup.length>0)
			userPrintSet.addAll(Arrays.asList(printGroup));
		

		Set<String> userViewSet = new HashSet<String>();
		if(viewGroup!=null&&viewGroup.length>0)
			userViewSet.addAll(Arrays.asList(viewGroup));
		
		
		Set<String> temp = new HashSet<String>();
		for(String username : userViewSet){
			if(userPrintSet.contains(username))
				temp.add(username);
		}
		if(temp.size()>0){
			userViewSet.removeAll(temp);
		}
		temp=null;
		
		Set<String> oldUserPrintSet =  new HashSet<String>();		
		if(oldPrintGroup!=null&&oldPrintGroup.length>0)
			oldUserPrintSet.addAll(Arrays.asList(oldPrintGroup));
		
		Set<String> oldUserViewSet =  new HashSet<String>();
		if(oldViewGroup!=null&&oldViewGroup.length>0)
			oldUserViewSet.addAll(Arrays.asList(oldViewGroup));
		
		List<UserActivityGroup> newActivityGroups = new ArrayList<UserActivityGroup>();
		List<UserActivityGroup> oldActivityGroups = new ArrayList<UserActivityGroup>();
		HashMap<String, List<UserActivityGroup>> userActivityMap = new HashMap<String, List<UserActivityGroup>>();
		
		for(String userName:userPrintSet){
			if(userName!=null){
				if(!oldUserPrintSet.contains(userName)){
					UserActivityGroup activityGroup = new UserActivityGroup();
					activityGroup.setActivityType(DocActivityType.Print);
					activityGroup.setdocId(activityForm.getDocId());
					activityGroup.setUsername(userName);
					newActivityGroups.add(activityGroup);
				}
			}
		}
		for(String userName:oldUserPrintSet){
			if(userName!=null){
				if(!userPrintSet.contains(userName)){
					UserActivityGroup activityGroup = new UserActivityGroup();
					activityGroup.setActivityType(DocActivityType.Print);
					activityGroup.setdocId(activityForm.getDocId());
					activityGroup.setUsername(userName);
					oldActivityGroups.add(activityGroup);
				}
			}
		}
		for(String userName:userViewSet){
			if(userName!=null){
				if(!oldUserViewSet.contains(userName)){
					UserActivityGroup activityGroup = new UserActivityGroup();
					activityGroup.setActivityType(DocActivityType.View);
					activityGroup.setdocId(activityForm.getDocId());
					activityGroup.setUsername(userName);
					newActivityGroups.add(activityGroup);
				}
			}
		}
		for(String userName:oldUserViewSet){
			if(userName!=null){
				if(!userViewSet.contains(userName)){
					UserActivityGroup activityGroup = new UserActivityGroup();
					activityGroup.setActivityType(DocActivityType.View);
					activityGroup.setdocId(activityForm.getDocId());
					activityGroup.setUsername(userName);
					oldActivityGroups.add(activityGroup);
				}
			}
		}			
		
		userActivityMap.put("add", newActivityGroups);
		userActivityMap.put("delete", oldActivityGroups);
		return userActivityMap;
	}

	public static List<DocumentLinkDTO> getDocumentLinkDtoObject(
			Set<String> linkedDocIdset, long docId) {
		List<DocumentLinkDTO> documentLinkDTOs = new ArrayList<DocumentLinkDTO>();
		for(String linkedDocId:linkedDocIdset)
		{
			DocumentLinkDTO documentLinkDTO = new DocumentLinkDTO();
			documentLinkDTO.setParentDocId(docId);
			documentLinkDTO.setLinkedDocId(Long.parseLong(linkedDocId));
			documentLinkDTO.setLinkType(DocLinkType.General);
			populateCommonDetails(documentLinkDTO, true);
			documentLinkDTOs.add(documentLinkDTO);
		}
		return documentLinkDTOs;
	}
	public static Map<String, List<DocumentLinkDTO>> documentLinkingUpdation(String[] linkedDocId,String[] oldLinkedDocId,long documentId)
	{
		List<DocumentLinkDTO> newLinkedDocIdlist = new ArrayList<DocumentLinkDTO>();
		List<DocumentLinkDTO> oldLinkedDocIdlist = new ArrayList<DocumentLinkDTO>();
		Map<String, List<DocumentLinkDTO>> documentLinkMap = new HashMap<String, List<DocumentLinkDTO>>();
		Set<String> linkedDocIdSet = new HashSet<String>();
		if(linkedDocId!=null&&linkedDocId.length>0){
			linkedDocIdSet.addAll(Arrays.asList(linkedDocId));
			linkedDocIdSet.remove(documentId);
		}
		Set<String> oldLinkedDocIdSet = new HashSet<String>();
		if(oldLinkedDocId!=null&&oldLinkedDocId.length>0)
			oldLinkedDocIdSet.addAll(Arrays.asList(oldLinkedDocId));
		for(String docId:linkedDocIdSet){
			if(docId!=null)
			{
				if(!oldLinkedDocIdSet.contains(docId)){
					DocumentLinkDTO documentLinkDTO = new DocumentLinkDTO();
					documentLinkDTO.setLinkedDocId(Long.parseLong(docId));
					documentLinkDTO.setParentDocId(documentId);
					documentLinkDTO.setLinkType(DocLinkType.General);
					populateCommonDetails(documentLinkDTO, true);
					newLinkedDocIdlist.add(documentLinkDTO);
				}
			}
		}
		for(String docId:oldLinkedDocIdSet){
			if(docId!=null){
				if(!linkedDocIdSet.contains(docId)){
					DocumentLinkDTO documentLinkDTO = new DocumentLinkDTO();
					documentLinkDTO.setLinkedDocId(Long.parseLong(docId));
					documentLinkDTO.setParentDocId(documentId);
					documentLinkDTO.setLinkType(DocLinkType.General);
					oldLinkedDocIdlist.add(documentLinkDTO);
				}
			}
		}
		documentLinkMap.put("add", newLinkedDocIdlist);
		documentLinkMap.put("delete", oldLinkedDocIdlist);
		return documentLinkMap;
	}
}
